from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

from .models import Blog

'''
重定向，以及根据url名称生成跳转的超链接；
from django.http import HttpResponseRedirect
from django.urls import reverse 此函数返回一个字符串。

在方法中
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

说明：对于有提交表单的情况，应该总是使用重定向，避免用户通过点击回退按钮产生重复提交。
'''


def index(request):
    return HttpResponse("Hello, world. You're at the app1 index.")
	
def ap1(request):
    return HttpResponse("Hello, world. You're at the app1 ap1.")

def ap2(request):
    return render(request, "test/test.html")

def apx(request):
    return HttpResponse("Hello, world. You're at the app1 apx.")

	
def dbSelect(request):
    blogs = Blog.objects.all()
    print(blogs)
    print(blogs[0])
    print(blogs[0].title)
    print(blogs[0].summary)
    # 得到的是 2019-06-11 22:57:39+00:00。
    print(blogs[0].created_at)
    return HttpResponse("Hello, world. You're at the app1 dbSelect.")

	
# 按主键
# blogs = Blog.objects.get(pk=主键值)
# 带有数据的渲染页面
# return render(request, "test/test.html", {'页面中使用的key': 数据或变量})